/**
 * 工资卡换绑
 * @author  Jello
 * @create  2018/10/18 14:08
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var sessionStorage = window.sessionStorage;
var localStorage = window.localStorage;
var spinner = new Spinner(opts);
var i = 0; //密码位数
var token = ""; //获取验证码成功后返回的token，用于验证验证码
var userid_sign = "";


$(function() {
	userid_sign = sessionStorage.getItem("userid_sign");
	if(!userid_sign){
		userid_sign = sessionStorage.getItem("openid");
	}
	
	//换绑信息赋值
	var decrypt = new JSEncrypt();
	decrypt.setPrivateKey(privateKey);　
	$("#userName").val(sessionStorage.getItem("bank_user_name"));
	$("#certNo").val(decrypt.decrypt(sessionStorage.getItem("identity_no")));
	$("#cardNo").val(decrypt.decrypt(sessionStorage.getItem("card_no")));
	$("#reservedPhone").val(sessionStorage.getItem("reserved_phone"));

	//换绑
	$("#changebind_btn").click(function() {
		backLogin(userid_sign, "../../login.html", function() {
			$(".hidden_yhk").css("display", "block");
			$(".paycar_box").css("display", "none");
			$(".qy_detas").val("工资卡换绑");
			$("#changebind_btn").css("display", "none");
		})
	})

	//数字显示隐藏
	$(".xiaq_tb").click(function() {
		$(".numb_box").slideUp(500);
	});
	$(".mm_box").click(function() {
		$(".numb_box").slideDown(500);
	});

	//提现密码身份校验
	$(".nub_ggg li .zf_num").click(function() {
		if(i < 6) {
			$(".mm_box li").eq(i).addClass("mmdd");
			$(".mm_box li").eq(i).attr("withpwd", $(this).text());
			i++;
			if(i == 6) {
				//判断本地存储密码次数为3次并且cookie中存储的值为null时,就把本地存储的次数清除(cookie10分钟失效后要把本地存储的密码次数删除)
				if(localStorage.getItem("sess_time_local") == 3 && $.cookie("sess_time_local") == null) {
					localStorage.removeItem("sess_time_local");
				}
				var sess_time = localStorage.getItem("sess_time_local"); //当前密码错误次数就等于本地存储的次数,牵扯到提现密码的页面都会记录并且每次叠加,直接赋值
				setTimeout(function() {
					var withpwd = "";
					$(".mm_box li").each(function() { //获取输入的密码
						withpwd += $(this).attr("withpwd");
					});
					if($.cookie("sess_time_local") == 3) { //密码错误次数过多
						$(".esc_box4").show();
						$(".esc_divbox4").show();
						return;
					} else {
						//校验提现密码接口上送
						var param = {};
						param.userid_sign = userid_sign;
						param.type = 1;
						param.oldPwd = hex_md5(hex_md5(withpwd) + salt);
						param.withdrawPwd = "";
						checkWithdrawPwd(param, function(resultObj) {
							if(resultObj.code == 0 && resultObj.data == 1) {
								$(".hidden_yhk").css("display", "none");
								$(".paycar_box").css("display", "block");
								$("#bind_btn").css("display", "block");
								$(".numb_box").css("display", "none");
								$("#cardNo").attr("disabled", false);
								$("#reservedPhone").attr("disabled", false);
								$("#gzk_img9").show();
								$("#gzk_img10").show();
								$.myToast("密码校验成功");
							} else {
								//身份验证失败,记录错误提现密码次数
								sess_time++;
								localStorage.setItem("sess_time_local", sess_time);
								if(sess_time == 3) { //错误次数达到3次
									$(".esc_divbox4").show();
									$(".esc_box4").show();
									var date = new Date();
									date.setTime(date.getTime() + 60 * 1000 * 10); //过期为10分钟
									$.cookie("sess_time_local", sess_time, { //存储次数cookie
										expires: date,
										path: '/bluehired'
									});
								} else { //密码错误次数提醒
									$("#local_time").html((3 - sess_time));
									$(".esc_box3").show();
									$(".esc_divbox3").show();
								}
							}
						});
					}
				}, 100);
			}
		}
	})

	//密码删除
	$(".nub_ggg li .zf_del").click(function() {
		if(i > 0) {
			i--
			$(".mm_box li").eq(i).removeClass("mmdd");
			$(".mm_box li").eq(i).attr("withpwd", "");
		}
	});
	//密码清空
	$(".nub_ggg li .zf_empty").click(function() {
		$(".mm_box li").removeClass("mmdd");
		$(".mm_box li").attr("withpwd", "");
		i = 0;
	});

	//提现密码错误重试
	$("#retrybtn").click(function() {
		$(".esc_box3").hide();
		$(".esc_divbox3").hide();
		$(".mm_box li").removeClass("mmdd");
		$(".mm_box li").attr("withpwd", "");
		i = 0;
	});
	//提现密码错误，忘记密码跳转，且存储手机号
	$("#forget_withdrawpwd").click(function() {
		sessionStorage.setItem("reg_phone", sessionStorage.getItem("reg_phone"));
		window.location.href = "withdrawpwd_updatecode.html";
	});
	//返回个人中心
	$("#backpersion_page").click(function() {
		if(sessionStorage.getItem("wallet_sign") == 001166) {
			window.location.href = "../../wallet.html";
		} else if(sessionStorage.getItem("paycard_sign") == 001177) {
			window.location.href = "../../paycardmanager_grade.html";
		} else {
			window.location.href = "../../app/personal/personal.html";
		}
	})

	//银行卡/预留手机input清空
	$("#gzk_img9").click(function() {
		$("#cardNo").val("");
		$("#gzk_img9").hide();
	});
	$("#gzk_img10").click(function() {
		$("#reservedPhone").val("");
		$("#gzk_img10").hide();
	});

	//验证身份且校验四要素通过,进行换绑
	$("#bind_btn").click(function() {
		var name = $("#userName").val();
		var certNo = $("#certNo").val();
		var cardNo = $("#cardNo").val();
		var phone = $("#reservedPhone").val();
		var checkFlag = checkBankCard(name, certNo, cardNo, phone, 2);

		if(checkFlag) {
			var encrypt = new JSEncrypt();
			encrypt.setPublicKey(publicKey)
			//工资卡换绑接口上送
			$.ajax({
				type: "post",
				url: serverUrl + "/userbank/bindunbind_bankcard?sgin=" + userid_sign,
				data: "{\"userName\":\"" + name + "\",\"identityNo\":\"" + encrypt.encrypt(certNo) + "\",\"bankNumber\":\"" + encrypt.encrypt(cardNo) + "\",\"bankUserTel\":\"" + phone + "\",\"moneyPassword\":\"" + "" + "\",\"type\":\"" + 2 + "\"}",
				cache: false,
				async: false,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				crossDomain: true,
				timeout: 15000,
				beforeSend: function() {
					$("#myspin").text("");
					var target = $("#myspin").get(0);
					spinner.spin(target);
				},
				success: function(data) {
					spinner.spin();
					var resultObj = eval(data);
					if(resultObj.code == 0 && resultObj.data != null) {
						if(resultObj.data.res_code == 0) {
							window.location.href = "paycardbinding_succ.html";
						} else {
							$("#errNum").html(resultObj.data.res_error_num);
							$("#errMsg").html(resultObj.data.res_msg);
							$(".esc_box").show();
							$(".esc_divbox").show();
						}
					} else if(resultObj.code == 20029) {
						$(".esc_box2").show();
						$(".esc_divbox2").show();
					} else {
						$.myToast(resultObj.msg);
						return;
					}
				},
				error: function(XMLHttpRequest, status, errorThrown) {
					spinner.spin();
					if(status == 'timeout') {
						$.myToast("请求超时");
					} else {
						$.myToast("请求网络服务数据失败");
					}
					return;
				}
			});
		}
	})

	//错误次数提示框
	$("#tm_ckdeil").click(function() {
		$(".esc_box").hide();
		$(".esc_divbox").hide();
	})

	//绑定机会已使用完提示框
	$("#tm_ckdeil2").click(function() {
		$(".esc_box2").hide();
		$(".esc_divbox2").hide();
	})

	//通过短信验证码方式完成身份验证事件
	$("#phoneMsg").click(function() {
		$("#phone").html(sessionStorage.getItem("reg_phone"));
		$(".numb_box").hide();
		$(".hidden_yhk").hide();
		$(".paycar_box").hide();
		$(".payiphEL").show();
	})

	//获取验证码
	$("#code_btn").click(function() {
		var phoneNum = $("#phone").html(); //手机号
		var param = {};
		param.phone = phoneNum;
		param.type = 2;
		//获取验证码接口上送
		gainCode(param, function(data) {
			token = data;
			//开启验证码定时器
			for(i = 1; i <= 60; i++) {
				if(i == 1) {
					window.setTimeout("countDown(" + i + ")");
				} else {
					window.setTimeout("countDown(" + i + ")", (i - 1) * 1000);
				}
			}
		});
	})

	//验证码确定
	$("#mate_btn").click(function() {
		var code = $("#get_code").val(); //验证码
		var phoneNum = $("#phone").html(); //手机号
		var flag = checkCode(code);
		if(flag) {
			//校验验证码接口上送
			var param = {};
			param.phone = phoneNum;
			param.type = 2;
			param.code = code;
			param.token = token;
			mateCode(param, function(data) {
				spinner.spin();
				//验证成功后把禁用的input去除,恢复可输入状态
				mui.toast("短信验证成功");
				$(".payiphEL").hide();
				$(".paycar_box").show();
				$("#bind_btn").show();
				$("#cardNo").attr("disabled", false);
				$("#reservedPhone").attr("disabled", false);
				$("#gzk_img9").show();
				$("#gzk_img10").show();
			})
		}
	})

})

/**
 * 校验卡号
 */
function checkCardNo() {
	var reg = /^(\d{16}|\d{19})$/;;
	var cardNo = $("#cardNo").val().trim();
	$("#gzk_img9").hide();
	if(!reg.test(cardNo)) {
		$("#gzk_img5").css("display", "block");
		$("#gzk_img6").css("display", "none");
	} else {
		$("#gzk_img5").css("display", "none");
		$("#gzk_img6").css("display", "block");
	}
}

/**
 * 校验预留手机号
 */
function checkPhone() {
	var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	var phone = $("#reservedPhone").val().trim();
	$("#gzk_img10").hide();
	if(!reg.test(phone)) {
		$("#gzk_img7").css("display", "block");
		$("#gzk_img8").css("display", "none");
	} else {
		$("#gzk_img7").css("display", "none");
		$("#gzk_img8").css("display", "block");
	}
}